938C - Constructing Tests - CodeForces Solution


binary search brute force constructive algorithms *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define PI acos(-1.0)
typedef long long ll; 

const int maxx = 1e9;

int solve(ll n,ll x){
	if(n*n <= x) return -1;
	ll i,j,k1,k2,low,high,mid;
	i = n*n;
	low = 2,high = n;
	while(low<=high){
		mid = (low+high)/2;
		j = n/mid;
		k1 = i - j*j;
		//k2 = i - (mid*mid);
		if(k1 == x) return mid;
		//else if(k2 == x) return j;
		else if(k1 < x){
			low = mid+1;
		}
		else if(k1 > x){
			high = mid-1;
		}
	}
	return -1;
}

int main(){
	ll t,i,j,k,x,n,m;
	cin >> t;
	while(t--){
		scanf("%lld",&x);
		if(x == 0){
			n = m = 1;
		}
		else{
			for(i=sqrtl(x);i<1e5;i++){
				if((i*i - (i/2)*(i/2)) > maxx){
					n = m = -1;
					break;
				}
				m = solve(i,x);
				if(m != -1){
					n = i;
					break;
				}
			}
		}
		if(n == -1){
			printf("-1\n");
		}
		else{
			printf("%lld %lld\n",n,m);
		}
	}
return 0;		
}


Comments

Submit
0 Comments
More Questions

1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points